package com.belle.sfsdfasdfsdf.service;

import com.belle.sfsdfasdfsdf.dao.SchedulerJobDao;
import com.belle.sfsdfasdfsdf.domain.ScheduleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 将任务数据持久化到数据库
 */
@Service
public class JobServiceImpl implements JobService {
	
	@Autowired
	private SchedulerJobDao schedulerJobDao;
	
	/**
	 * 添加一个job信息到数据库
	 * @author sxl
	 * @date 2018/9/18 0018 14:19
	 * @param scheduleJob
	 * @return void
	 */
	public int add(ScheduleJob scheduleJob) {
		ScheduleJob saved = schedulerJobDao.save(scheduleJob);
		if(saved == null) {
			return 0;
		}
		return 1;
	}
	
	/**
	 * 更新一个job信息到数据库
	 * @author sxl
	 * @date 2018/9/18 0018 14:19
	 * @param scheduleJob
	 * @return int
	 */
	public int update(ScheduleJob scheduleJob) {
		ScheduleJob updated = schedulerJobDao.save(scheduleJob);
		if(updated == null) {
			return 0;
		}
		return 1;
	}
	
	/**
	 * 查询全部可用
	 * @author sxl
	 * @date 2018/9/18 0018 14:22
	 * @param
	 * @return java.util.List<com.belle.sfsdfasdfsdf.domain.ScheduleJob>
	 */
	@Override
	public List<ScheduleJob> findAllAvailable() {
		return schedulerJobDao.findAllByJobStatus(1);
	}
	
	@Override
	public ScheduleJob findByJobNameAndJobGroup(String jobName, String jobGroup) {
		if(StringUtils.isEmpty(jobGroup) || StringUtils.isEmpty(jobName)) {
			return null;
		}
		return schedulerJobDao.findByJobNameAndJobGroup(jobName, jobGroup);
	}
	
	@Override
	public int delete(String jobName, String jobGroup) {
		if(StringUtils.isEmpty(jobGroup) || StringUtils.isEmpty(jobName)) {
			return 0;
		}
		ScheduleJob.PrimaryKey pk = new ScheduleJob.PrimaryKey();
		pk.setJobName(jobName);
		pk.setJobGroup(jobGroup);
		schedulerJobDao.deleteById(pk);
		return 1;
	}
}
